//-----------------------------------------------------------------------------
//  File          : hardware.c
//  Module        :
//  Description   : Include hardware handle routines.
//  Author        : Lewis
//  Notes         : N/A
//
// ============================================================================
// | Naming conventions                                                       |
// | ~~~~~~~~~~~~~~~~~~                                                       |
// |         Struct/Array : Leading s                                         |
// |             Constant : Leading K                                         |
// |      Global Variable : Leading g  (variable use outside one C file)      |
// |                                                                          |
// |   BYTE( 8 Bits) size : Leading b                                         |
// |   CHAR( 8 Bits) size : Leading c                                         |
// |   WORD(16 Bits) size : Leading w                                         |
// |  DWORD(32 Bits) size : Leading d                                         |
// |    int(32 Bits) size : Leading i                                         |
// | DDWORD(64 Bits) size : Leading dd                                        |
// |              Pointer : Leading p                                         |
// |                                                                          |
// |    Function argument : Leading a                                         |
// |       Local Variable : All lower case                                    |
// |                                                                          |
// | Examples:                                                                |
// |                Array : Leading s, (sb = arrary of byte, etc)             |
// |              Pointer : Leading p, (pb = pointer of byte, etc)            |
// ============================================================================
//
//  Date          Who         Action
//  ------------  ----------- -------------------------------------------------
//  01 Apr  2009  Lewis       Initial Version.
//-----------------------------------------------------------------------------
#include <string.h>
#include "midware.h"
#include "sysutil.h"
#include "kbdutil.h"
#include "hardware.h"

//-----------------------------------------------------------------------------
//      Defines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//      Globals
//-----------------------------------------------------------------------------
// Device Handle
int    gDevHandle[APM_SDEV_MAX];
int    gTimerHdl[MAX_TIMER_SUPPORT];

//-----------------------------------------------------------------------------
//      Constants
//-----------------------------------------------------------------------------

//*****************************************************************************
//  Function        : InitAllHw
//  Description     : Initialize all hardware drivers.
//  Input           : N/A
//  Return          : N/A
//  Note            : N/A
//  Globals Changed : N/A
//*****************************************************************************
void InitAllHw(void)
{
  BYTE filename[32];
  int i;

  for (i=0; i < APM_SDEV_MAX; i++) {
    gDevHandle[i] = -1;
  }

  strcpy(filename, DEV_MSR1);
  gDevHandle[APM_SDEV_MSR] = OpenMW(filename, MW_RDONLY);
  strcpy(filename, DEV_ICC1);
  gDevHandle[APM_SDEV_ICC] = OpenMW(filename, MW_RDWR);
  strcpy(filename, DEV_GPRS);
  gDevHandle[APM_SDEV_GPRS] = OpenMW(filename, MW_RDWR);
  strcpy(filename, DEV_PPP1);
  gDevHandle[APM_SDEV_PPP] = OpenMW(filename, MW_RDWR);

  for (i=0; i < MAX_TIMER_SUPPORT; i++) {
    gTimerHdl[i] = TimerOpenMW();
  }
#if (T800)  
  os_disp_backc(0x7B36);
#endif
  
  bs_lan_open();//++zx 110906
  
  // !2009-11-02++
  // Requirement from Fire
  //ClearKeyin();
  //Delay10ms(300);
  //WaitKey(90000, 0);
  // !2009-11-02--
  
  //if (bs_lan_open()) {
  //  DispPutStr("\x10\f\n  Detect LAN Cable  \n <CANCEL> To Skip");
  //  TimerSetMW(gTimerHdl[TIMER_GEN], 300);  // wait 3 secs for LAN cable detect
  //  while (GetCharMW()!=MWKEY_CANCL) {
  //    if (bs_lan_status(0) & K_LanPlugIn)
  //      break;
  //    if (TimerGetMW(gTimerHdl[TIMER_GEN]) == 0) {  // save power
  //      bs_lan_close();
  //      break;
  //    }
  //  }
  //}
}
//*****************************************************************************
//  Function        : CloseAllHw
//  Description     : Close All hardware driver.
//  Input           : N/A
//  Return          : N/A
//  Note            : N/A
//  Globals Changed : N/A
//*****************************************************************************
void CloseAllHw(void)
{
  int i;

  for (i=0; i < APM_SDEV_MAX; i++) {
    if (gDevHandle[i] >= 0)
      CloseMW(gDevHandle[i]);
  }

  for (i=0; i < MAX_TIMER_SUPPORT; i++) {
    if (gTimerHdl[i] >=0)
      CloseMW(gTimerHdl[i]);
  }
  
  bs_lan_close();//++zx 110906
}
//*****************************************************************************
//  Function        : GetHwHandle
//  Description     : Return hardware handle id.
//  Input           : aHwId;
//  Return          : handle;
//  Note            : N/A
//  Globals Changed : N/A
//*****************************************************************************
int GetHwHandle(DWORD aHwId)
{
  if (aHwId < APM_SDEV_MAX)
    return gDevHandle[aHwId];

  return -1;
}

